import type { FormRules } from 'element-plus'
import type { FormData } from '~/types/user'

export const createFormRules = (formData: FormData): FormRules => ({
  phone: [
    { required: true, message: '请输入手机号码' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名' },
    { min: 2, max: 20, message: '姓名长度在2-20个字符之间' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号码' },
    {
      pattern: /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9X]$/,
      message: '请输入正确的身份证号码'
    }
  ],
  nickname: [
    { required: true, message: '请输入昵称' },
    { min: 2, max: 20, message: '昵称长度在2-20个字符之间' }
  ],
  oldPassword: [
    { required: true, message: '请输入当前密码' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码' },
    { pattern: /^(?=.*[a-zA-Z])(?=.*[0-9@#$%&]).{8,12}$/, message: '密码需为8~12个字符，至少包含字母和数字或特殊符号' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码' },
    {
      validator: (rule: any, value: string, callback: Function) => {
        if (value !== formData.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      }
    }
  ],
  steamId: [
    { required: true, message: '请输入Steam ID' }
  ],
  apiKey: [
    { required: true, message: '请输入API key' }
  ],
  tradeUrl: [
    { required: true, message: '请输入交易链接' },
    { pattern: /^https:\/\/steamcommunity\.com\/tradeoffer\/new\//, message: '请输入正确的Steam交易链接' }
  ],
  alipayPhone: [
    { required: true, message: '请输入支付宝绑定手机号码' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码' }
  ],
  steamUsername: [
    { required: true, message: '请输入Steam账号', trigger: 'blur' }
  ],
  steamPassword: [
    { required: true, message: '请输入Steam密码', trigger: 'blur' }
  ],
  steamGuardCode: [
    { required: true, message: '请输入Steam令牌验证码', trigger: 'blur' },
    { min: 5, max: 5, message: '令牌验证码必须是5位', trigger: 'blur' }
  ]
})